package com.zhao.dynamicProxy.JDKProxy.proxy;


import com.zhao.dynamicProxy.JDKProxy.RealSubject.TrainStation;
import com.zhao.dynamicProxy.JDKProxy.Subject.SellTickets;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactory{

    private static SellTickets trainStation = new TrainStation();

    public static SellTickets getTrainStation() {
        SellTickets sellTickets = (SellTickets) Proxy.newProxyInstance(trainStation.getClass().getClassLoader(),
                trainStation.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("Proxy ... ");
                        Object invoke = method.invoke(trainStation, args);
                        return invoke;
                    }
                });
        return sellTickets;
    }
}
